package com.wfk.h5.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wfk.entity.Goods;
import com.wfk.entity.OrderGoods;
import com.wfk.h5.service.GoodsOrderService;
import com.wfk.service.IGoodsService;
import com.wfk.service.IOrderGoodsService;
import com.wfk.h5.mapping.MappingUtils;
import com.wfk.utils.BasePage;
import com.wfk.utils.ResponseData;

@Controller
public class GoodsOrderController {
	@Autowired
	private GoodsOrderService goodsOrderService;
	@Autowired
	private IGoodsService goodsService;
	@Autowired
	private IOrderGoodsService orderGoodsService;
	
	@RequestMapping(MappingUtils.goodsOrder)
	@ResponseBody
	public ResponseData handleConfirmRecei(Integer id){
		try{
			return this.goodsOrderService.handleConfirmRecei(id);
		}catch(Exception e){
			e.printStackTrace();
			return ResponseData.baseServiceError("网络连接错误");
		}
	}
	
	@RequestMapping(MappingUtils.cancelOrder)
	@ResponseBody
	public ResponseData cancelOrder(Integer id){
		try{
			return this.goodsOrderService.cancelOrder(id);
		}catch(Exception e){
			e.printStackTrace();
			return ResponseData.baseServiceError("网络连接错误");
		}
	}
	
	/**
	 * @param page
	 * @param cid
	 * @param status
	 * @return
	 */
	@RequestMapping(MappingUtils.goodsOrderDetail)
	@ResponseBody
	public ResponseData goodsOrderDetail(Integer id){
		try{
			return ResponseData.ok().putDataValue("result",this.orderGoodsService.getById(id));
		}catch(Exception e){
			e.printStackTrace();
			return ResponseData.baseServiceError("网络连接错误");
		}
	}
	
	/**
	 * @param page
	 * @param cid
	 * @param status
	 * @return
	 */
	@RequestMapping(MappingUtils.myGoodsOrder)
	@ResponseBody
	public ResponseData myGoods(BasePage<OrderGoods> page,OrderGoods orderGoods){
		try{
			if(orderGoods.getCid()==null) {
				return ResponseData.badRequest();
			}
			return ResponseData.ok().putDataValue("result",this.orderGoodsService.getList(page,orderGoods));
		}catch(Exception e){
			e.printStackTrace();
			return ResponseData.baseServiceError("网络连接错误");
		}
	}

	@RequestMapping(MappingUtils.orderGoodsAddPre)
	@ResponseBody
	public ResponseData orderGoodsAddPre(String goodsCode,Integer qty,Integer addrid,String payword,Integer spec,String remark,Integer paytype){
		try{
			return this.goodsOrderService.addOrderGoods(goodsCode, qty, addrid, payword,spec,true,remark,paytype);
		}catch(Exception e){
			e.printStackTrace();
			return ResponseData.baseServiceError("网络连接错误");
		}
	}
	
	@RequestMapping(MappingUtils.orderGoodsAdd)
	@ResponseBody
	public ResponseData addOrderGoods(String goodsCode,Integer qty,Integer addrid,String payword,Integer spec,String remark,Integer paytype){
		try{
			return this.goodsOrderService.addOrderGoods(goodsCode, qty, addrid, payword,spec,false,remark,paytype);
		}catch(Exception e){
			e.printStackTrace();
			return ResponseData.baseServiceError("网络连接错误");
		}
	}
	
	@RequestMapping(MappingUtils.queryGoodsAll)
	@ResponseBody
	public ResponseData queryGoodsAll(BasePage<Goods> page,Goods goods){
		try{
			goods.setStatus(1);
			return ResponseData.ok().putDataValue("result", this.goodsService.getList(goods,page));
		}catch(Exception e){
			e.printStackTrace();
			return ResponseData.baseServiceError("网络连接错误");
		}
	}
	
	/**
	 * 商品详情
	 * @param goodsCode
	 * @return
	 */
	@RequestMapping(MappingUtils.getGoodsDetail)
	@ResponseBody
	public ResponseData getGoodsDetail(String goodsCode){
		try{
			return ResponseData.ok().putDataValue("result",this.goodsService.getGoodsDetail(goodsCode));
		}catch(Exception e){
			e.printStackTrace();
			return ResponseData.baseServiceError("网络连接错误");
		}
	}
}
